{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 1\n",
    "def remainder(number, divisor):\n",
    "    return number % divisor\n",
    "\n",
    "assert remainder(20, 7) == 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "6\n",
      "6\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "print(remainder(20, 7))\n",
    "print(remainder(20, divisor=7))\n",
    "print(remainder(number=20, divisor=7))\n",
    "print(remainder(divisor=7, number=20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 3\n",
    "try:\n",
    "    # This will not compile\n",
    "    source = \"\"\"remainder(number=20, 7)\"\"\"\n",
    "    eval(source)\n",
    "except:\n",
    "    logging.exception('Expected')\n",
    "else:\n",
    "    assert False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 4\n",
    "try:\n",
    "    remainder(20, number=7)\n",
    "except:\n",
    "    logging.exception('Expected')\n",
    "else:\n",
    "    assert False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.167 kg per second\n"
     ]
    }
   ],
   "source": [
    "# Example 5\n",
    "def flow_rate(weight_diff, time_diff):\n",
    "    return weight_diff / time_diff\n",
    "\n",
    "weight_diff = 0.5\n",
    "time_diff = 3\n",
    "flow = flow_rate(weight_diff, time_diff)\n",
    "print('%.3f kg per second' % flow)\n",
    "assert (flow - 0.16666666666666666) < 0.0001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 6\n",
    "def flow_rate(weight_diff, time_diff, period):\n",
    "    return (weight_diff / time_diff) * period"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 7\n",
    "flow_per_second = flow_rate(weight_diff, time_diff, 1)\n",
    "assert (flow_per_second - 0.16666666666666666) < 0.0001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 8\n",
    "def flow_rate(weight_diff, time_diff, period=1):\n",
    "    return (weight_diff / time_diff) * period"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 9\n",
    "flow_per_second = flow_rate(weight_diff, time_diff)\n",
    "assert (flow_per_second - 0.16666666666666666) < 0.0001\n",
    "flow_per_hour = flow_rate(weight_diff, time_diff, period=3600)\n",
    "assert flow_per_hour == 600.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 10\n",
    "def flow_rate(weight_diff, time_diff,\n",
    "              period=1, units_per_kg=1):\n",
    "    return ((weight_diff * units_per_kg) / time_diff) * period"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1320.0\n"
     ]
    }
   ],
   "source": [
    "# Example 11\n",
    "pounds_per_hour = flow_rate(weight_diff, time_diff,\n",
    "                            period=3600, units_per_kg=2.2)\n",
    "print(pounds_per_hour)\n",
    "assert pounds_per_hour == 1320.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1320.0\n"
     ]
    }
   ],
   "source": [
    "# Example 12\n",
    "pounds_per_hour = flow_rate(weight_diff, time_diff, 3600, 2.2)\n",
    "print(pounds_per_hour)\n",
    "assert pounds_per_hour == 1320.0"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
